<template>
	<view class="page">
		<view class="bar_info" :style="{height:(88+StatusBarRpx)+'rpx',paddingTop:StatusBarRpx+'rpx'}">
			<view class="title_bar">
				<view class="title_bar_left">
					<image :src='STATIC_URL+"7.png"' mode="aspectFill"></image>
					<text>洛阳聚肴商贸有限公司</text>
				</view>
				<view class="title_bar_right"></view>
			</view>
		</view>
		<view :style="{height: (88+StatusBarRpx)+'rpx'}"></view>
		<view class="search_info" @tap.stop="handleJump" data-type="navigateTo" :data-url="`/pages/index/search`">
			<image class="search_icon" :src='STATIC_URL+"9.png"' mode="aspectFill"></image>
			<input class="search_input" type="text" placeholder="输入商品关键词"
				placeholder-style="font-size: 26rpx;color: #8B8B8B;">
		</view>
		
		<view class="loading_css" v-if="isShow">
			<u-loading size="36" color="#41AF36"></u-loading>
			<view class="loading_text">加载中</view>
		</view>
		<block v-else>
			<view class="classification">
				<scroll-view :scroll-x="true" class="classification_list">
					<view class="classification_item" v-for="(item,index) in categoryList" @tap.stop="selectOne(item.id, index)">
						<image :src='item.image' mode="aspectFill"></image>
						<view class="classification_item_text" :class="category_one_id == item.id ? 'active_text' : ''">{{item.label}}</view>
					</view>
				</scroll-view>
			</view>
			<view class="classification_info" :style="{height: windowHeight - (415 + StatusBarRpx) + 'rpx'}" v-if="categoryChild.length > 0">
				<scroll-view :scroll-y="true" class="classification_info_le">
					<view class="classify_text" v-for="(item, index) in categoryChild" :key="index" @tap.stop="selectTwo(item.id, index)">
						<view class="classify_icon" v-if="category_two_id == item.id"></view>
						<view :class="category_two_id == item.id ? 'classify_active' : ''">{{item.label}}</view>
					</view>
				</scroll-view>
				<scroll-view :scroll-y="true" class="classification_info_ri">
					<view class="loading_view" v-if="is_two_show">
						<u-loading size="36" color="#41AF36"></u-loading>
						<view>加载中</view>
					</view>
					<template v-else>
						<view class="shop_item flex" v-for="(item, index) in list" :key="index" @tap.stop="handleJump" data-type="navigateTo" :data-url="`/pages/index/goodDetails?id=${item.id}`">
							<view class="item_left_img">
								<u-lazy-load :image="item.image" img-mode="aspectFill" height="170"
									border-radius="8"></u-lazy-load>
							</view>
							<view class="item_right">
								<view class="shop_name">{{item.name}}</view>
								<view class="specification" v-if="item.specification">规格：{{item.specification}}</view>
								<view class="shop_price flex align-center justify-between">
									<view class="price_text">
										<template v-if="user.is_show_price == 1">
											<text class="price_1">￥</text>
											<text class="price_2">{{item.price}}</text>
											<text class="price_3">/{{filterDictionary(item.unit)}}</text>
										</template>
										<template v-else>
											<text class="price_3">单位：{{filterDictionary(item.unit)}}</text>
										</template>
									</view>
									<view class="shop_num flex align-center justify-end" @tap.stop="changeNum(item)">
										<template v-if="item.count > 0">
											<image :src='STATIC_URL+"19.png"' mode="aspectFill"></image>
											<text class="shop_num_input">{{item.count}}</text>
										</template>
										<image :src='STATIC_URL+"20.png"' mode="aspectFill"></image>
									</view>
								</view>
							</view>
						
						</view>
						<view class="noData_other" style="margin-top: 200rpx;" v-if="list.length == 0 && !is_two_show">
							<image src="/static/empty/no.png"></image>
						</view>
					</template>
				</scroll-view>
			</view>
			<view class="noData" v-if="categoryChild.length == 0 && !isShow">
				<image src="/static/empty/no.png"></image>
			</view>
		</block>
		
		<cart-popup :source="1" ref="cart" :item="shopItemInfo" @changeShop="changeShopInfo"></cart-popup>
	</view>
</template>

<script>
	import cartPopup from "@/components/cartPopup/cartPopup.vue"

	export default {
		data() {
			return {
				isShow: true,
				is_two_show: true,
				list: [],
				image: '/static/index/3.png',
				cardIndex: 0,
				token: "",
				windowHeight: 0,
				categoryList: [],  // 一级分类列表
				category_one_id: "",  // 一级分类id
				categoryChild: [],  // 二级分类列表
				category_two_id: "",  // 二级分类id
				shopItemInfo: "",
				user: {
					is_show_price: 0
				}
			}
		},
		components: {
			cartPopup
		},
		computed: {

		},
		watch: {
		},
		onLoad(options) {
			setTimeout(() => {
				this.windowHeight = this.pxToRpx(uni.getSystemInfoSync().windowHeight);
			}, 300)
			this.getCategory();
			uni.$on('cartChange', res => {
				this.getCategory();
			})
		},
		onShow() {
			this.user = global.userInfo;
			// console.log(this.user);
			this.token = global.token;
			this.category_one_id = global.category_id;
		},
		onUnload() {
			uni.$off('cartChange');
		},
		// 下拉刷新
		onPullDownRefresh() {
			
		},
		methods: {
			changeNum(item) {
				this.shopItemInfo = item;
				setTimeout(() => {
					this.$refs.cart.openCart();
				}, 100)
			},
			// px转rpx
			pxToRpx(px) {
				let scale = uni.upx2px(100) / 100;
				return px / scale
			},
			// 分类列表  glyCategory
			getCategory() {
				this.$api.get(global.apiUrls.glyCategory, {
					is_home: 0
				}).then(res => {
					// console.log(res);
					if (res.data.code == 200) {
						this.categoryList = res.data.data;
						if (this.categoryList.length > 0) {
							let index = 0;
							if (!this.category_one_id) {
								this.category_one_id = this.categoryList[0].id;
								global.category_id = this.category_one_id;
							} else {
								this.categoryList.forEach((v, k) => {
									if (this.category_one_id == v.id) {
										index = k;
									}
								})
							}
							this.categoryChild = this.categoryList[index].children;
							if (this.categoryChild) {
								if (this.categoryChild.length > 0) {
									this.category_two_id = this.categoryChild[0].id;
									this.getShopData();
								}
							} else {
								this.categoryChild = [];
								this.isShow = false;
							}
						}
						
					} else {
						this.$message.info(res.data.message);
					}
				})
			},
			// 商品列表  glyShopList
			getShopData() {
				this.isShow = true;
				this.is_two_show = true;
				this.$api.get(global.apiUrls.glyShopList, {
					category_id: this.category_two_id
				}).then(res => {
					this.isShow = false;
					this.is_two_show = false;
					if (res.data.code == 200) {
						this.list = res.data.data;
						let cartList = this.$store.state.cartList;
						if (cartList.length > 0) {
							cartList.forEach((v, k) => {
								this.list.forEach((v1, k1) => {
									if (v.id == v1.id) {
										v1.count = v.count
									}
								})
							})
						}
					} else {
						this.$message.info(res.data.message);
					}
				})
			},
			changeShopInfo(e) {
				let index = this.list.findIndex(item => item.id == e.id);
				this.list[index].count = e.count;
			},
			selectOne(id, index) {
				this.category_one_id = id;
				global.category_id = this.category_one_id;
				this.categoryChild = this.categoryList[index].children;
				if (this.categoryChild) {
					if (this.categoryChild.length > 0) {
						this.category_two_id = this.categoryChild[0].id;
						this.getShopData();
					}
				} else {
					this.categoryChild = [];
				}
				
			},
			selectTwo(id, index) {
				this.category_two_id = id;
				this.getShopData();
			},
		}
	}
</script>

<style lang="scss" scoped>
	.loading_view {
		text-align: center;
		position: absolute;
		z-index: 100;
		left: 50%;
		top: 50%;
		transform: translate(-50%, -50%);
		view {
			color: #999999;
			font-size: 22rpx;
			margin-top: 16rpx;
		}
	}
	.page {
		background: url(https://www.shiyaocanyin.com/images/8.png) no-repeat #f6f6f6;
		background-size: 100% auto !important;
		min-height: calc(100vh - var(--window-bottom));
		.bar_info {
			width: 750rpx;
			height: 88rpx;
			overflow: hidden;
			// border-bottom: 1px solid #eee;
			// background: #fff;
			z-index: 10;
			position: fixed;
			top: 0rpx;
			display: flex;
			background: url(https://www.shiyaocanyin.com/images/8.png) no-repeat;
			background-size: 100% auto;

			.title_bar {
				padding: 0 32rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.title_bar_left {
					display: flex;
					align-items: center;

					image {
						width: 36rpx;
						height: 36rpx;
					}

					text {
						font-weight: 500;
						font-size: 30rpx;
						color: #FFFFFF;
						margin-left: 10rpx;
					}
				}

				.title_bar_center {
					line-height: 88rpx;
					font-size: 36rpx;
					font-weight: 400;
					color: #fff;
				}

				.title_bar_right {}
			}
		}

		.search_info {
			margin: 10rpx 25rpx 0;
			width: 700rpx;
			height: 70rpx;
			background: #fff;
			border-radius: 100rpx;
			position: relative;

			.search_icon {
				width: 30rpx;
				height: 30rpx;
				position: absolute;
				left: 30rpx;
				top: 20rpx;
			}

			.search_input {
				height: 70rpx;
				display: flex;
				flex: 1;
				padding: 0 32rpx 0 80rpx;
			}
		}

		.search_label {
			overflow: hidden;
			margin: 15rpx 25rpx 0;

			.search_label_item {
				display: inline-block;
				padding: 1px 20rpx;
				background: rgba(255, 255, 255, 0.2);
				border-radius: 100rpx;
				font-weight: 400;
				font-size: 24rpx;
				color: #FFFFFF;
				margin: 0 20rpx 20rpx 0;
			}
		}

		.classification {
			margin: 40rpx 25rpx 0;

			.classification_list {
				white-space: nowrap;

				.classification_item {
					display: inline-block;
					text-align: center;
					margin: 0 48rpx 0 0;

					image {
						width: 90rpx;
						height: 90rpx;
						border-radius: 50%;
					}

					.classification_item_text {
						font-weight: 400;
						font-size: 24rpx;
						color: #3D3D3D;
						margin-top: 10rpx;
						white-space: nowrap;
					}

					.active_text {
						padding: 1px 20rpx;
						background: #41AF36;
						font-size: 24rpx;
						border-radius: 100rpx;
						color: #FFFFFF;
					}
				}
			}
		}

		.tips_info {
			padding: 30rpx 0;

			image {
				width: 108rpx;
				height: 5rpx;
			}

			.tips_text {
				font-weight: 500;
				font-size: 36rpx;
				color: #3D3D3D;
				margin: 0 20rpx;
			}
		}

		.classification_info {
			margin-top: 45rpx;
			display: flex;

			.classification_info_le {
				height: 100%;
				width: 190rpx;
				// background: #F6F6F6;
				.classify_text {
					font-weight: 400;
					font-size: 26rpx;
					color: rgba(61,61,61,0.5);
					padding: 27rpx 10rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					position: relative;
					.classify_icon {
						position: absolute;
						left: 0;
						top: 0;
						width: 6rpx;
						height: 100%;
						background: #41AF36;
					}
					
				}
			}
			.classify_active {
				font-weight: 500;
				font-size: 30rpx;
				color: #3D3D3D;
			}

			.classification_info_ri {
				width: calc(100% - 190rpx);
				height: 100%;
				padding: 0 20rpx 20rpx;
				background: linear-gradient(to bottom, transparent, #fff);
				position: relative;
			}
		}
		.shop_classify {
			font-weight: 400;
			font-size: 26rpx;
			color: #3D3D3D;
			margin-bottom: 30rpx;
		}
		.shop_item {
			margin-bottom: 20rpx;

			.item_left_img {
				width: 170rpx;
				height: 170rpx;
			}

			/deep/.u-lazy-item {
				width: 170rpx !important;
				height: 170rpx !important;
			}

			.item_right {
				width: calc(100% - 180rpx);
				padding: 0 0 0 30rpx;

				.shop_name {
					height: 80rpx;
					font-weight: 500;
					font-size: 32rpx;
					color: #3D3D3D;
					line-height: 40rpx;
					word-break: break-all;
					text-overflow: ellipsis;
					overflow: hidden;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2; /* 这里是超出几行省略 */
				}
				.specification {
					color: #999;
					font-size: 24rpx;
				}

				.shop_price {
					margin-top: 14rpx;
					.price_text {
						.price_1 {
							color: #D43C3B;
							font-size: 24rpx;
							font-weight: 600;
						}

						.price_2 {
							color: #D43C3B;
							font-size: 36rpx;
							font-weight: 600;
						}

						.price_3 {
							color: #989898;
							font-size: 24rpx;
							font-weight: 500;
						}
					}

					.shop_num {
						image {
							width: 60rpx;
							height: 40rpx;
						}

						.shop_num_input {
							text-align: center;
							margin: 0 12rpx;
							font-weight: 400;
							font-size: 24rpx;
							color: #3D3D3D;
						}
					}
				}
			}
		}

		.swiper_box {
			padding: 38rpx 30rpx;
		}

		.swiper_nav {
			background: #fff;
			position: relative;

			swiper {
				height: 345rpx;
				// margin-bottom: 32rpx;
			}

			.swiper-item {
				display: flex;
				flex-wrap: wrap;
				justify-content: flex-start;
				align-items: center;

				.swiper-item_box {
					width: 25%;
					height: 130rpx;
					text-align: center;
					margin-bottom: 42rpx;

					image {
						width: 80rpx;
						height: 80rpx;
						margin: 0 auto;
					}

					.swiper-item_box_text {
						font-size: 26rpx;
						margin-top: 16rpx;
					}
				}
			}

			.swiper_dot {
				// position: absolute;
				// bottom: 0;
				// left: 50%;
				// transform: translateX(-50%);
				display: flex;
				justify-content: center;
				padding: 16rpx 0;

				.dot_item {
					display: inline-block;
					width: 12rpx;
					height: 12rpx;
					border-radius: 50%;
					background: #0278FC;
					opacity: 0.2;
					margin: 0 8rpx;
				}

				.active_dot_item {
					opacity: 1;
				}
			}
		}

	}
</style>